﻿/*
 * @Autor: PK
 * @Date: 2021-03-31 16:36:36
 * @Description: Vehicle实现类
 */

using System;
using System.Collections.Generic;
using ShkSimulation.core.dao.statistic;
using ShkSimulation.core.template.agency;
using ShkSimulation.carsharing;

namespace ShkSimulation.carsharing.impl.elements {

    public class Vehicle : CarsharingVehicle , DataReporter{

        public string uniquekey;

        public string Uniquekey => uniquekey;
        
        public DateTime StartChargeTime { get; set; }
        
        public Vehicle(int id, string identify, double fullBattery = 300, double initBattery = 200) : base(id) {
            this.FullBattery = fullBattery;
            this.Battery = initBattery;
            this.uniquekey = id.ToString();
            this.Identify = identify;
        }
        
        public double FullBattery { get; private set; }
        public Agency CureentAgency { get; private set; }
        public double Battery { get; set; }
        public double Distance { get; set; }
        public double Price { get; internal set; }
        public double FuelCost { get; internal set; }
        public DateTime TimeStopBilling { get; internal set; }
        public DateTime TimeArriveStation { get; internal set; }
        public DateTime TimeToArriveRoad { get; internal set; }
        public DateTime TimeToLeaveStation { get; internal set; }
        public double Income { get; set; }
        public double TotalCost { get; set; }

        /**
        * @description: DataReport
        */
        public event Action<DataReporter> ReportToCollection = _ => { };
        
        private Dictionary<string, double> reportRelatedDoubleData = new Dictionary<string, double>();
        
        public DataReportInfo GetReportInfo() {
            reportRelatedDoubleData["单车总收入"] = Income;
            reportRelatedDoubleData["单车总成本"] = TotalCost;
            reportRelatedDoubleData["单车净利润"] = Income - TotalCost;
            return DataReportInfo.Create(Uniquekey, DateTime.MinValue, null, reportRelatedDoubleData);
        }

        public override bool BatteryEnough(DateTime clockTime, double demand) {
            double p1 = 4.211 * Math.Pow(10, -16);
            double p2 = -2.207 * Math.Pow(10, -11);
            double p3 = 4.197 * Math.Pow(10, -7);
            double p4 = -0.003377;
            double p5 = 10.33;
            double currentBattery = Battery / 300;  //currentbattery 为初始电量   
            TimeSpan span = clockTime - StartChargeTime;
            double t = span.TotalSeconds;
            double t0 = ((currentBattery - 0.01) * 3600 * 40) / 10;//假设从初始电量充到当前电量剩余所需要的时间
            double linearcharge = 0.01 + 10 * span.TotalSeconds / (3600 * 40);
            double judge = currentBattery + linearcharge;
            if (judge <= 0.8) {
                currentBattery += linearcharge;
            } else {
                currentBattery = p1 * Math.Pow(t + t0, 4) + p2 * Math.Pow(t + t0, 3) + p3 * Math.Pow(t + t0, 2) + p4 * (t + t0) + p5;
            }
            StartChargeTime = clockTime;
            return Battery >= demand;
        }
        public override void UpdateBattery() {
            this.Battery -= Distance;
        }

        public override void SkipAgency(int count) {
            throw new NotImplementedException();
        }

        public override void ExistNetwok() {
            
        }

    }
}
